package com.face.util;

import java.io.StringWriter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    private String jsonStr;
    private static Logger log = LogManager.getLogger(JsonUtil.class);



    public JsonUtil(String jsonStr){
        this.jsonStr = jsonStr;
    }

    public String jsonGetParam(String name){
        try {
            JSONObject content = new JSONObject(jsonStr);
            return content.getString(name);
        } catch (JSONException e) {
            log.error(e);
            return null;
        }
    }

    public static String objectToJsonStr(Object obj) throws Exception {
        if (null == obj)
            return new JSONObject().toString();
        StringWriter str = new StringWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(str, obj);
        return str.toString();
    }
    
    

}
